import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_2b_client/apis/MarketOrderApi.dart';
import 'package:flutter_2b_client/utils/SingleUnitPeriod.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';

import '../apis/GoodsApi.dart';
import '../components/InfoList.dart';
import '../components/LoadableIconButton.dart';

class GoodsDetailPage extends HookWidget {
  final int id;

  const GoodsDetailPage({Key? key, required this.id}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var goodsDetailState = useState<GoodsDetail?>(null);
    queryDetail(){
      goodsApi.getGoodsDetail(id).then((value) {
        debugPrint(value.toString());
        goodsDetailState.value = value;
      });
    }
    useEffect(() {
      queryDetail();
      return null;
    }, [id]);
    final basicUnit = useMemoized(
        () => goodsDetailState.value?.units
            .firstWhereOrNull((it) => it.basicMultiple == 0),
        [goodsDetailState.value]);

    return Scaffold(
      appBar: AppBar(
        title: const Text("商品详情"),
        actions: [
          IconButton(
            icon: const Icon(Icons.edit),
            onPressed: ()async {
              var result = await context.push("/goods/$id/edit");
              if(result!=null && result==true){
                queryDetail();
              }
            },
          ),
        ],
      ),
      body: goodsDetailState.value == null
          ? Container()
          : SingleChildScrollView(
              child: InfoList([
                InfoItemOption(
                  title: "名称",
                  content: goodsDetailState.value!.name,
                  suffix: buildCopyableButton(goodsDetailState.value!.name),
                ),
                InfoItemOption(
                  title: "条码",
                  content: goodsDetailState.value!.barcode,
                  suffix: buildCopyableButton(goodsDetailState.value!.barcode),
                ),
                InfoItemOption(
                  title: "零售价",
                  content: goodsDetailState.value!.price.toString(),
                ),
                InfoItemOption(
                  title: "库存",
                  content: goodsDetailState.value!.amount!.toString(),
                ),
                if (goodsDetailState.value?.boxSpecification != null)
                  InfoItemOption(
                    title: "箱规",
                    content: goodsDetailState.value!.name,
                  ),
                if (goodsDetailState.value?.unit != null)
                  InfoItemOption(
                    title: "单位",
                    content: goodsDetailState.value!.name,
                  ),
                if (goodsDetailState.value?.category != null)
                  InfoItemOption(
                    title: "类型",
                    content: goodsDetailState.value!.category!.name,
                  ),
                if (goodsDetailState.value?.factory != null)
                  InfoItemOption(
                    title: "厂商",
                    content: goodsDetailState.value!.factory!.name,
                  ),
                if (goodsDetailState.value?.brand != null)
                  InfoItemOption(
                    title: "品牌",
                    content: goodsDetailState.value!.brand!.name,
                  ),
                if (basicUnit != null)
                  InfoItemOption(
                    title: "基本单位",
                    content: basicUnit.name,
                  ),
                if (goodsDetailState.value?.expressionPeriod != null)
                  InfoItemOption(
                    title: "保质期",
                    content: SingleUnitPeriod.parse(goodsDetailState.value!.expressionPeriod!).toDisplayString(),
                  ),
                if (goodsDetailState.value?.recordFilingNumber?.isNotEmpty==true)
                  InfoItemOption(
                    title: "备案编号",
                    content: goodsDetailState.value!.recordFilingNumber!,
                  ),
                if (goodsDetailState.value?.registrationCertificateNumber?.isNotEmpty==true)
                  InfoItemOption(
                    title: "注册证编号",
                    content: goodsDetailState.value!.registrationCertificateNumber!,
                  ),
                if (goodsDetailState.value?.recordFilingNumber?.isNotEmpty==true)
                  InfoItemOption(
                    title: "备案编号",
                    content: goodsDetailState.value!.recordFilingNumber!,
                  ),
                if (goodsDetailState.value?.executiveStandard?.isNotEmpty==true)
                  InfoItemOption(
                    title: "执行编号",
                    content: goodsDetailState.value!.executiveStandard!,
                  ),
                if ((goodsDetailState.value?.units.length ?? 0) > 1)
                  ...goodsDetailState.value?.units.sublist(1).map((e) =>
                          InfoItemOption(
                              title: "辅助单位",
                              content:
                                  "${e.name}${basicUnit == null ? "" : "=${e.basicMultiple}${basicUnit.name}"}")) ??
                      []
              ]),
            ),
    );
  }

  LoadableIconButton buildCopyableButton(String value) {
    return LoadableIconButton(
      icon: const Icon(Icons.copy),
      onPressed: () async {
        await Clipboard.setData(ClipboardData(text: value));
      },
    );
  }
}
